//
//  LSZVideoOfflineCacheVC+LCHomeBaiduAd.m
//  JKFuYinProject
//
//  Created by admin on 2018/7/23.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZVideoOfflineCacheVC+LCHomeBaiduAd.h"

#define ADID_TYPE1  @"5865791"//底部大图+文字
//普通原生5864785
// 视频信息流原生：5864786
//4393166" //组图
#define PublisherId  @"b3a6d662"

@implementation LSZVideoOfflineCacheVC (LCHomeBaiduAd)

#pragma mark - 广告相关设置
//请求广告
- (void)pressToLoadAd
{
    [self baiduAdDealloc];
    if (!self.native)
    {
        self.native = [[BaiduMobAdNative alloc]init];
        self.native.delegate = self;
    }
    //请求原生广告
    [self.native requestNativeAds];
}

-(NSString*)publisherId
{
    return PublisherId;
}

//创建后获得的信息流广告位id
-(NSString*)apId
{
    return ADID_TYPE1;//ADID_NORMAL; //普通信息流, 3条
}

//广告返回成功
//成功返回广告字段，BaiduMobAdNativeAdObject array
- (void)nativeAdObjectsSuccessLoad:(NSArray*)nativeAds{
    
    self.adViewArray = [NSMutableArray array];
    self.adsArray = [nativeAds copy];
    for(int i = 0; i < [nativeAds count]; i++){
        BaiduMobAdNativeAdObject *object = [nativeAds objectAtIndex:i];
        CGFloat lcBaiduMobAdHeight;
        if (ScreenWidth == 320) {
            lcBaiduMobAdHeight = 86.5;
        }
        else if (ScreenWidth == 375) {
            lcBaiduMobAdHeight = 98;
        }
        else if (ScreenWidth == 414) {
            lcBaiduMobAdHeight = 106;
        }
        else {
            lcBaiduMobAdHeight = 106;
        }
        lcBaiduMobAdHeight = lcBaiduMobAdHeight - 2;//高度小于cell高度,否则不显示线
        BaiduMobAdNativeAdView *view = [self createNativeAdViewWithframe:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, lcBaiduMobAdHeight) object:object];
        //传入用来展示广告详情页的viewcontroller (也可以不传)
        view.presentAdViewController = self;
        if (object.materialType == NORMAL) {
            [self.adViewArray addObject:view];
        }
        
        // 展现前检查是否过期，30分钟广告将过期，如果广告过期，请放弃展示并重新请求
        if ([object isExpired]) {
            continue;
        }
//        @weakify(self)
        // 加载和显示广告内容
        [view loadAndDisplayNativeAdWithObject:object completion:^(NSArray *errors) {
            if (!errors) {
//                @strongify(self)
                // 确定视图显示在window上之后再调用trackImpression，不要太早调用
                //在tableview或scrollview中使用时尤其要注意
            }
        }];
    }
}

//广告返回失败
-(void)nativeAdsFailLoad:(BaiduMobFailReason)reason
{
    NSLog(@"nativeAdsFailLoad,reason = %d",reason);
//    self.adViewArray = [NSMutableArray array];
//    [self.tableView reloadData];
}

//广告被点击，打开后续详情页面，如果为视频广告，可选择暂停视频
-(void)nativeAdClicked:(UIView *)nativeAdView
{
    if ([nativeAdView isKindOfClass:[BaiduMobAdNativeAdView class]]) {
        BaiduMobAdNativeAdView *bdAdView = (BaiduMobAdNativeAdView*) nativeAdView;
        if ([bdAdView.videoView isKindOfClass:[BaiduMobAdNativeVideoView class]]) {
            [(BaiduMobAdNativeVideoView *)bdAdView.videoView pause];
        }
    }
    NSLog(@"nativeAdClicked");
}

//广告详情页被关闭，如果为视频广告，可选择继续播放视频
-(void)didDismissLandingPage:(UIView *)nativeAdView
{
    if ([nativeAdView isKindOfClass:[BaiduMobAdNativeAdView class]]) {
        BaiduMobAdNativeAdView *bdAdView = (BaiduMobAdNativeAdView*) nativeAdView;
        if ([bdAdView.videoView isKindOfClass:[BaiduMobAdNativeVideoView class]]) {
            [(BaiduMobAdNativeVideoView *)(bdAdView.videoView) play];
        }
    }
    NSLog(@"didDismissLandingPage");
}

#pragma mark - adConfig
- (BaiduMobAdNativeAdView *)createNativeAdViewWithframe:(CGRect)frame object:(BaiduMobAdNativeAdObject *)object
{
        UILabel *brandLabel =
    [[UILabel alloc]initWithFrame:CGRectMake(155, 5, 212, 20)];
    brandLabel.font = [UIFont fontWithName:brandLabel.font.familyName size:15];
    
    UILabel *titleLabel =
    [[UILabel alloc]initWithFrame:CGRectMake(155, 50, 200, 10)];
    titleLabel.font = [UIFont fontWithName:titleLabel.font.familyName size:14];
    
    UILabel *textLabel =
    [[UILabel alloc] initWithFrame:CGRectMake(155, 10, 200, 20)];
//    if (ScreenWidth == 414) {
//        textLabel.font = [UIFont fontWithName:textLabel.font.familyName size:(14.5)];
//    }else {
//        if (ScreenWidth == 375) {
            textLabel.font = [UIFont fontWithName:textLabel.font.familyName size:(14)];
//        }else{
//            textLabel.font = [UIFont fontWithName:textLabel.font.familyName size:(12)];
//        }
//    }
    textLabel.numberOfLines = 2;
    textLabel.textColor = titleColorLSZ;
    UIImageView *iconImageView =
    [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 100, 80)];
    iconImageView.layer.masksToBounds = YES;
    // 设置圆角大小
    iconImageView.layer.cornerRadius = 7.0 ;

    UIImageView *mainImageView =
    [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 80)];
    
    UILabel *adLogoLabel = [[UILabel alloc] initWithFrame:CGRectMake(286, 318, 24, 12)];

    BaiduMobAdNativeAdView *nativeAdView;
    
    if (object.materialType == NORMAL) {
        brandLabel = nil;//广告标识
//        titleLabel = nil;//广告Title
        textLabel = nil;
//                iconImageView = nil;
//                mainImageView = nil;
        
        nativeAdView =  [[BaiduMobAdNativeAdView alloc]initWithFrame:frame
                                                           brandName:brandLabel
                                                               title:titleLabel
                                                                text:textLabel
                                                                icon:iconImageView
                                                           mainImage:mainImageView];
        [nativeAdView addSubview:adLogoLabel];

    } else if (object.materialType == VIDEO) {
        BaiduMobAdNativeVideoView *videoView = [[BaiduMobAdNativeVideoView alloc]initWithFrame:CGRectMake(0, 80, 320, 180) andObject:object];
        
        mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 80, 300, 180)];
        //在大图上添加播放按钮引导用户点击视频
        UIImage *image = [self imageResoureForName:@"play_big_image"];
        UIImageView *playImage = [[UIImageView alloc ]initWithImage:image];
        playImage.alpha = 0.85;
        playImage.frame = CGRectMake(86, 40, 120, 120);
        [mainImageView addSubview:playImage];
        
        
        nativeAdView = [[BaiduMobAdNativeAdView alloc] initWithFrame:frame
                                                           brandName:brandLabel
                                                               title:titleLabel
                                                                text:textLabel
                                                                icon:iconImageView
                                                           mainImage:mainImageView
                                                           videoView:videoView
                        ];
        
    } else if (object.materialType == HTML) {
        //#warning 信息流模版广告
        //#warning 模板广告内部已添加百度广告logo和熊掌，开发者无需添加
        BaiduMobAdNativeWebView *webview = [[BaiduMobAdNativeWebView alloc]initWithFrame:frame andObject:object];
        nativeAdView = [[BaiduMobAdNativeAdView alloc]initWithFrame:frame
                                                            webview:webview];
    }
    
    nativeAdView.backgroundColor = [UIColor clearColor];
    
    [mainImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nativeAdView).offset(10);
        make.height.equalTo(@(80));
        make.width.equalTo(mainImageView.mas_height).multipliedBy(1.25);
        make.top.equalTo(nativeAdView).offset(10);
    }];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(mainImageView);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(mainImageView.mas_right).offset(9.5);
        make.top.equalTo(mainImageView.mas_top).offset(0);
        make.right.equalTo(nativeAdView).offset(-10);
        make.height.equalTo(@(40));
    }];
    adLogoLabel.font = FONT_10;
    adLogoLabel.text = @"广告";
    adLogoLabel.textColor = RGBColor(166, 166, 166);
    [adLogoLabel sizeToFit];
    [nativeAdView addSubview:adLogoLabel];
    [adLogoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left).offset(5);
        make.width.and.height.lessThanOrEqualTo(adLogoLabel);
        make.bottom.equalTo(mainImageView.mas_bottom).offset(-5-10);
    }];
    UIView *adLogoLabelBGBiew = [[UIView alloc] init];
    [adLogoLabel addSubview:adLogoLabelBGBiew];
    
    adLogoLabelBGBiew.layer.masksToBounds = YES;
    adLogoLabelBGBiew.layer.cornerRadius = 7.0 ;
    adLogoLabelBGBiew.layer.borderColor = RGBColor(166, 166, 166).CGColor;//设置边框颜色
    adLogoLabelBGBiew.layer.borderWidth = 1.0f;//设置边框颜色

    [adLogoLabelBGBiew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(adLogoLabel.mas_left).offset(-5);
        make.top.equalTo(adLogoLabel.mas_top).offset(-2.5);
        make.right.equalTo(adLogoLabel.mas_right).offset(5);
        make.bottom.equalTo(adLogoLabel.mas_bottom).offset(2.5);
    }];

    UIButton *butDelete = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 10 - 40, 0, 50, 100)];
    [butDelete setImage:[UIImage imageNamed:@"我的_下载_删除"] forState:UIControlStateNormal];
    [nativeAdView addSubview:butDelete];

    return nativeAdView;
}

- (UIImage *)imageResoureForName:(NSString*)name
{
    NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"baidumobadsdk" ofType:@"bundle"];
    NSBundle* b=  [NSBundle bundleWithPath:bundlePath];
    return [UIImage imageWithContentsOfFile: [b pathForResource:name ofType:@"png"]];
}
//-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
//    if (self.adViewArray.count > indexPath.row)
//    {
//        BaiduMobAdNativeAdView *view = [self.adViewArray objectAtIndex:indexPath.row];
//        view.tag = 0;
//        [[cell viewWithTag:0] removeFromSuperview];
//        [cell addSubview: view];
//        [self sendVisibleImpressionAtIndexPath:indexPath];
//    }
//    return cell;
//}

//- (void)sendVisibleImpressionAtIndexPath:(NSIndexPath *)indexPath adIndex:(NSInteger)adIndex
//{
//    
//    NSArray *visiblePath = [self.tableViewVideo indexPathsForVisibleRows];
//    if ([visiblePath containsObject:indexPath]) {
//        if ([self.adViewArray count] > adIndex) {
//            BaiduMobAdNativeAdView *view = [self.adViewArray objectAtIndex:adIndex];
//            
//            //仍然支持旧版本的展现方法
//            //            [view trackImpression];
//            BaiduMobAdNativeAdObject *object = [self.adsArray objectAtIndex:adIndex];
//            [object trackImpression:view];
//        }
//    }
//}

///**
// * 大图高度，仅用于信息流模版广告
// */
//-(NSNumber*)baiduMobAdsHeight {
//    return [NSNumber numberWithFloat:180];
//}
//
///**
// * 大图宽度，仅用于信息流模版广告
// */
//-(NSNumber*)baiduMobAdsWidth {
//    return [NSNumber numberWithFloat:320];
//}

- (BOOL)enableLocation {
    return YES;
    
}

- (void)baiduAdDealloc {
    self.native.delegate = nil;
    self.native = nil;
    for (UIView *adview in self.adViewArray) {
        [adview removeFromSuperview];
    }
    [self.adViewArray removeAllObjects];
    self.adViewArray = nil;
    self.adsArray = nil;
}

@end
